﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace WindowsFormsApplication1
{
    class TextBoxHelper
    {
        bool textEntered = false;
        string helpStr;

        public static void InitHelp(TextBox textBox, string helpText)
        {
            TextBoxHelper helper = new TextBoxHelper();
            helper.helpStr = helpText;
            textBox.ForeColor = Color.Gray;
            textBox.Text = helpText;

            textBox.Enter += helper.textBox_Enter;
            textBox.Leave += helper.textBox_Leave;
            textBox.TextChanged += helper.textBox_TextChanged;

            textBox.Tag = helper;
        }

        public static bool HasText(TextBox textBox)
        {
            return ((TextBoxHelper)textBox.Tag).textEntered;
        }

        private void textBox_Enter(object sender, EventArgs e)
        {
            TextBox textBox =((TextBox)sender);
            textBox.ForeColor = Color.Black;
            if (!textEntered)
            {
                textBox.Text = "";
            }
        }

        private void textBox_Leave(object sender, EventArgs e)
        {
            TextBox textBox = ((TextBox)sender);
            if (!textEntered)
            {
                textBox.ForeColor = Color.Gray;
                textBox.Text = helpStr;
                textEntered = false;
            }
        }

        private void textBox_TextChanged(object sender, EventArgs e)
        {
            TextBox textBox = ((TextBox)sender);
            textEntered = !string.IsNullOrEmpty(textBox.Text);
        }
    }
}
